﻿using Hotel.Models;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
using System;

namespace Hotel.Fileter
{
    public class CustomActionFilterAttribute : Attribute, IActionFilter
    {
        public void OnActionExecuted(ActionExecutedContext context)
        {

        }

        public void OnActionExecuting(ActionExecutingContext context)
        {
            //判断当前用户是否登录，没有登录去登录页面
            string userName = context.HttpContext.Session.GetString("employeeNo");
            //session中没有用户信息，用户没登录
            if (string.IsNullOrEmpty(userName))
            {
                string path = context.HttpContext.Request.Path.Value;
                path = path.ToLower();
                if (path.Contains("view") || path.Contains("index") || path == "/")
                {
                    context.Result = new RedirectResult("/employeeview/login");
                }
                else
                {
                    context.Result = new JsonResult(new ResultModel
                    {
                        Msg = "用户未登录"
                    });
                }

            }
        }
    }
}
